package com.qf.sms.controller;

import com.qf.base.result.R;
import com.qf.base.dto.sms.SmsDTO;
import com.qf.sms.service.SmsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author dsh
 * 处理短信的controller
 */
@RestController
@RequestMapping("qf/sms")
public class SmsController {


    @Resource
    private SmsService smsService;

    @PostMapping("send")
    public R send(@RequestBody SmsDTO smsDTO) {
        String phoneNo = smsDTO.getPhoneNo();
        String code = smsDTO.getCode();
        Integer serviceId = smsDTO.getServiceId();

        String msg = smsDTO.getMsg();

        //调用第三方业务
        //发送短信给phoneNO smsBody 短信内容

        if (code == null || "".equals(code)) {
            //短信验证码
            smsService.saveSms(phoneNo, code, serviceId);
        }

        return R.ok("短信验证码已经发送");
    }

    @PostMapping("check")
    public R check(@RequestBody SmsDTO smsDTO) {

        String phoneNo = smsDTO.getPhoneNo();
        String code = smsDTO.getCode();
        Integer serviceId = smsDTO.getServiceId();
        int result = smsService.checkSms(phoneNo, code, serviceId);
        if (result == 0) {
            return R.ok("短信验证码正确");
        } else if (result == 1) {
            return R.error(501, "短信验证码超时");
        } else if (result == 2) {
            return R.error(502, "请使用最新的验证码");
        } else  {
            return R.error(503,"验证码错误");
        }
    }
}
